<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="24">
          <el-form-item :label="$t('基金名称')" prop="fundName">
            <template>
              <router-link
                target="_blank"
                :to="{path:'/fund/fundBasehome',query:{id:form.fundId}}"
                class="link_a"
              >{{ form.fundName }}
              </router-link>
            </template>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('预计完成注销时间')" prop="estimatedCancellationTime">
            <el-date-picker
              v-model="form.estimatedCancellationTime"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('预计完成注销时间')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('其他风险说明')" prop="otherRiskStatements">
            <el-input
              v-model="form.otherRiskStatements"
              type="textarea"
              :placeholder="$t('其他风险说明')"
              show-word-limit
              maxlength="1000"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input
              v-model="form.remark"
              type="textarea"
              :placeholder="$t('备注')"
              show-word-limit
              maxlength="1000"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <FileTable
        :list.sync="form.fileList"
        data-type="fund_cancellation"
        :business-id="autoBusinessId($route.query.id, form)"
        :btn-show="!otherBtnHide"
      />
    </el-form>
  </div>
</template>

<script>
import { saveFundCancellation, getFundCancellation } from '@/api/fund/cancellation/fundCancellation'
import { getFundName } from '@/api/fund/fundBase'

export default {
  data() {
    return {
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        estimatedCancellationTime: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ]
      },
      // 遮罩层
      loading: false,
      // 操作按钮默认不展示
      buttonShow: true,
      otherBtnHide: false
    }
  },
  created() {
    this.$bus.$emit('async_done')
  },
  methods: {
    // 初始化数据
    init(data) {
      this.loading = true
      if (data != null && data.id !== undefined) {
        getFundCancellation(data.id).then(response => {
          this.form = response.data
          this.loading = false
        }).catch(() => {
          this.loading = false
        })
      } else {
        this.reset()
        if (data != null) {
          this.form.fileList = data.fileList
        }
        getFundName(this.$route.query.id).then(response => {
          this.form.fundName = response
          this.loading = false
        }).catch(() => {
          this.loading = false
        })
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        fundId: this.$route.query.id,
        fundName: undefined,
        estimatedCancellationTime: undefined,
        otherRiskStatements: undefined,
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      if (this.$parent.$parent.formOpen !== undefined) {
        this.$parent.$parent.formOpen = false
      }
      // 刷新list页面
      if (this.$parent.$parent.getList !== undefined) {
        this.$parent.$parent.getList()
      }
    },
    /** 提交按钮 */
    submitForm: function() {
      return new Promise((resolve, reject) => {
        this.$refs['form'].validate(valid => {
          // 验证数据
          if (valid) {
            // 弹出遮罩层
            this.loading = true
            // 保存数据
            saveFundCancellation(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess(this.$t('保存成功'))
                this.loading = false
                resolve(response.data.id)
              } else {
                this.msgError(response.msg)
                this.loading = false
                reject(response)
              }
            }).catch(() => {
              this.loading = false
            })
          } else {
            this.locationError(document)
            reject()
          }
        })
      })
    }
  }

}
</script>
